package com.yuesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuesystem.common.BaseContext;
import com.yuesystem.common.CustomException;
import com.yuesystem.entity.AcademyItem;
import com.yuesystem.entity.ChangeItem;
import com.yuesystem.entity.ChangeItem1;
import com.yuesystem.mapper.ChangeItemMapper;
import com.yuesystem.service.AcademyItemService;
import com.yuesystem.service.AcademyService;
import com.yuesystem.service.ChangeItem1Service;
import com.yuesystem.service.ChangeItemService;
import com.yuesystem.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 改变书院条目接口实现类.
 */
@Service
@Slf4j
public class ChangeItemServiceImpl extends ServiceImpl<ChangeItemMapper, ChangeItem>
    implements ChangeItemService {
  @Autowired
  private AcademyItemService academyItemService;
  
  @Autowired
  private AcademyService academyService;
  
  @Autowired
  private UserService userService;
  
  @Autowired
  private ChangeItem1Service changeItem1Service;
  
  /**
   * 增加条目信息，保存在academy_item表和change_item表中.
   *
   */
  @Override
  @Transactional
  public void addItem(AcademyItem academyItem) {
    if (academyItem.getObject().isEmpty() || academyItem.getSubject().isEmpty()
        || academyItem.getRelation().isEmpty()) {
      throw new CustomException("新增条目不能为空！");
    }
    // 查询数据库中有没有重复的该书院的条目
    LambdaQueryWrapper<AcademyItem> query = new LambdaQueryWrapper<>();
    query.eq(AcademyItem::getObject, academyItem.getObject())
        .eq(AcademyItem::getSubject, academyItem.getSubject())
        .eq(AcademyItem::getRelation, academyItem.getRelation())
        .eq(AcademyItem::getAcademyId, academyItem.getAcademyId())
        .eq(AcademyItem::getIsDeleted, 0);
    AcademyItem one = academyItemService.getOne(query);
    if (one != null) {
      throw new CustomException("该条目已存在，请重新输入！");
    } else {
      academyItemService.save(academyItem);
    }
    // 获得该增加条目的id
    LambdaQueryWrapper<AcademyItem> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(AcademyItem::getSubject, academyItem.getSubject())
        .eq(AcademyItem::getObject, academyItem.getObject())
        .eq(AcademyItem::getRelation, academyItem.getRelation())
        .eq(AcademyItem::getAcademyId, academyItem.getAcademyId())
        .eq(AcademyItem::getIsDeleted, 0);
    AcademyItem item = academyItemService.getOne(queryWrapper);
    
    log.info("增加条目信息:{}", item);
    
    // 获得当前登录用户id
    Long currentId = BaseContext.getCurrentId();
    
    ChangeItem1 changeItem1 = new ChangeItem1();
    changeItem1.setSubjectNew(academyItem.getSubject());
    changeItem1.setObjectNew(academyItem.getObject());
    changeItem1.setRelationNew(academyItem.getRelation());
    changeItem1.setUserId(currentId);
    changeItem1.setOperate("增加");
    log.info("增加操作信息 :{}", changeItem1);
    changeItem1Service.save(changeItem1);
  }
  
  /**
   * 删除academy_item表中该条目的信息，保存用户删除信息在change_item表中.
   */
  @Override
  @Transactional
  public void deleteItem(AcademyItem academyitem) {
    // 从academy_item表中删除该条目的信息
    academyitem.setIsDeleted(1);
    
    // 保存修改后的academyitem信息
    academyItemService.updateById(academyitem);
    
    ChangeItem1 changeItem1 = new ChangeItem1();
    changeItem1.setSubjectNew(academyitem.getSubject());
    changeItem1.setObjectNew(academyitem.getObject());
    changeItem1.setRelationNew(academyitem.getRelation());
    changeItem1.setUserId(BaseContext.getCurrentId());
    changeItem1.setOperate("删除");
    
    log.info("新增删除信息： {}", changeItem1);
    
    changeItem1Service.save(changeItem1);
  }
  
  /**
   * 修改书院条目信息，对change_item和academy_item表同时进行操作.
   */
  @Override
  @Transactional
  public void updateItem(AcademyItem academyItem) {
    if (academyItem.getObject().isEmpty() || academyItem.getRelation().isEmpty()) {
      throw new CustomException("修改条目不能为空！");
    }
    Long itemId = academyItem.getId();
    AcademyItem item = academyItemService.getItemById(itemId);
    
    // change_item表的操作
    ChangeItem1 changeItem1 = new ChangeItem1();
    changeItem1.setSubjectOld(item.getSubject());
    changeItem1.setObjectOld(item.getObject());
    changeItem1.setRelationOld(item.getRelation());
    changeItem1.setSubjectNew(academyItem.getSubject());
    changeItem1.setObjectNew(academyItem.getObject());
    changeItem1.setRelationNew(academyItem.getRelation());
    changeItem1.setUserId(BaseContext.getCurrentId());
    changeItem1.setOperate("修改");
    changeItem1Service.save(changeItem1);
    academyItemService.updateById(academyItem);
    
  }
  
  
}
